package api

import (
	"getaway/basic/config"
	__ "getaway/basic/proto/orderproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"getaway/middleware"
	"github.com/gin-gonic/gin"
)

// OrderStatisticsReq
// @Summary      按日统计订单数据
// @Description  按日统计订单数据
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query  request.OrderStatisticsReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/orderStatisticsReq [get]
// @Security TokenAuth
func OrderStatisticsReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.OrderStatisticsReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.OrderStatistics(c, &__.OrderStatisticsReq{
		Data: req.Date,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按日统计订单数据行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按日统计订单数据", map[string]interface{}{
		"date":    req.Date,
		"success": err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// OrderStatisticsRangeReq
// @Summary      周日期统计订单数据
// @Description  周日期统计订单数据
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.OrderStatisticsRangeReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/orderStatisticsRangeReq [get]
// @Security TokenAuth
func OrderStatisticsRangeReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.OrderStatisticsRangeReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.OrderClient.WeekOrderStatistics(c, &__.WeekOrderStatisticsReq{
		StartTime: req.StartDate,
		EndTime:   req.EndDate,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录周日期统计订单数据行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "周日期统计订单数据", map[string]interface{}{
		"startDate": req.StartDate,
		"endDate":   req.EndDate,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", res)
}

// OrderStatisticsMonthReq
// @Summary      按月统计订单数据
// @Description  按月统计订单数据
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.OrderStatisticsRangeReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/orderStatisticsMonthReq [get]
// @Security TokenAuth
func OrderStatisticsMonthReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.OrderStatisticsRangeReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.MonthOrderStatistics(c, &__.MonthOrderStatisticsReq{
		StartTime: req.StartDate,
		EndTime:   req.EndDate,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按月统计订单数据行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按月统计订单数据", map[string]interface{}{
		"startDate": req.StartDate,
		"endDate":   req.EndDate,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// OrderStatisticsYearReq
// @Summary      按年统计订单数据
// @Description  按年统计订单数据
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.OrderStatisticsRangeReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/orderStatisticsYearReq [get]
// @Security TokenAuth
func OrderStatisticsYearReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.OrderStatisticsRangeReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.YearOrderStatistics(c, &__.YearOrderStatisticsReq{
		StartTime: req.StartDate,
		EndTime:   req.EndDate,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按年统计订单数据行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按年统计订单数据", map[string]interface{}{
		"startDate": req.StartDate,
		"endDate":   req.EndDate,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// DateAverageOrderAmount
// @Summary      获取某天订单平均金额
// @Description  获取某天订单平均金额
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.OrderAvgReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/dateAverageOrderAmount [get]
// @Security TokenAuth
func DateAverageOrderAmount(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.OrderAvgReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.OrderClient.DateAverageOrderAmount(c, &__.DateAverageOrderAmountReq{
		Date:      req.Date,
		PayWay:    req.PayWay,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录获取某天订单平均金额行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "获取某天订单平均金额", map[string]interface{}{
		"date":      req.Date,
		"payWay":    req.PayWay,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", res)
}

// MonthOrderAvgPriceReq
// @Summary      按月统计订单平均价
// @Description  按月统计订单平均价
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.DateAverageOrderAmountReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/monthOrderAvgPriceReq [get]
// @Security TokenAuth
func MonthOrderAvgPriceReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.DateAverageOrderAmountReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.MonthOrderAvgPrice(c, &__.MonthOrderAvgPriceReq{
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		PayWay:    req.PayWay,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按月统计订单平均价行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按月统计订单平均价", map[string]interface{}{
		"startTime": req.StartTime,
		"endTime":   req.EndTime,
		"payWay":    req.PayWay,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// WeekOrderAvgPriceReq
// @Summary      按周统计订单平均价
// @Description  按周统计订单平均价
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.DateAverageOrderAmountReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/weekOrderAvgPriceReq [get]
// @Security TokenAuth
func WeekOrderAvgPriceReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.DateAverageOrderAmountReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.WeekAverageOrderAmount(c, &__.WeekAverageOrderAmountReq{
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		PayWay:    req.PayWay,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按周统计订单平均价行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按周统计订单平均价", map[string]interface{}{
		"startTime": req.StartTime,
		"endTime":   req.EndTime,
		"payWay":    req.PayWay,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// YearOrderAvgPriceReq
// @Summary      按年统计订单平均价
// @Description  按年统计订单平均价
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.DateAverageOrderAmountReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/yearOrderAvgPriceReq [get]
// @Security TokenAuth
func YearOrderAvgPriceReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.DateAverageOrderAmountReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.YearAverageOrderAmount(c, &__.YearAverageOrderAmountReq{
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		PayWay:    req.PayWay,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按年统计订单平均价行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按年统计订单平均价", map[string]interface{}{
		"startTime": req.StartTime,
		"endTime":   req.EndTime,
		"payWay":    req.PayWay,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// DateBetweenAverageOrderAmount
// @Summary      按范围日期统计订单平均价
// @Description  按范围日期统计订单平均价
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        summerize  query      request.DateAverageOrderAmountReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/dateBetweenAverageOrderAmount [get]
// @Security TokenAuth
func DateBetweenAverageOrderAmount(c *gin.Context) {

	var req request.DateAverageOrderAmountReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.OrderClient.DateBetweenAverageOrderAmount(c, &__.DateBetweenAverageOrderAmountReq{
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		PayWay:    req.PayWay,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按范围日期统计订单平均价行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按范围日期统计订单平均价", map[string]interface{}{
		"startTime": req.StartTime,
		"endTime":   req.EndTime,
		"payWay":    req.PayWay,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", res)
}

// PurchaseConversionReq
// @Summary      购买转化分析统计
// @Description  购买转化分析统计
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.PurchaseConversionReq    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/purchaseConversion [get]
// @Security TokenAuth
func PurchaseConversionReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.PurchaseConversionReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.GetPurchaseConversion(c, &__.GetPurchaseConversionReq{
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		OrderType: req.OrderType,
		PayWay:    req.PayWay,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录购买转化分析统计行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "购买转化分析统计", map[string]interface{}{
		"startTime": req.StartTime,
		"endTime":   req.EndTime,
		"orderType": req.OrderType,
		"payWay":    req.PayWay,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// OrderGrowthRateReq
// @Summary      订单增长率计算
// @Description  订单增长率计算
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.OrderAvgReq    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/orderGrowthRate [get]
// @Security TokenAuth
func OrderGrowthRateReq(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.OrderAvgReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.OrderGrowthRate(c, &__.OrderGrowthRateReq{
		Date:      req.Date,
		PayWay:    req.PayWay,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录订单增长率计算行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "订单增长率计算", map[string]interface{}{
		"date":      req.Date,
		"payWay":    req.PayWay,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}

// GetRepurchaseByInterval
// @Summary      按时间间隔统计复购人数
// @Description  按时间间隔统计复购人数
// @Tags         订单数据模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.GetRepurchaseByIntervalReq    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /summerize/getRepurchaseByInterval [get]
// @Security TokenAuth
func GetRepurchaseByInterval(c *gin.Context) {
	authorityId := c.GetUint("authorityId")
	if authorityId != 3 {
		response.ParameterError(c, "权限错误", "只有管理员才能查询")
		return
	}
	var req request.GetRepurchaseByIntervalReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	resp, err := config.OrderClient.GetRepurchaseByInterval(c, &__.GetRepurchaseByIntervalReq{
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		PayWay:    req.PayWay,
		OrderType: req.OrderType,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	// 记录按时间间隔统计复购人数行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "按时间间隔统计复购人数", map[string]interface{}{
		"startTime": req.StartTime,
		"endTime":   req.EndTime,
		"payWay":    req.PayWay,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "查询成功", resp)
}
